#include<bits/stdc++.h>
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<48||ch>57){if(ch=='-') f=-1;ch=getchar();}
	while(ch>=48&&ch<=57) x=x*10+ch-48,ch=getchar();
	return x*f;
}
int prime[8000],len;
bool vis[60005];
void sss()
{
	vis[0]=vis[1]=1;
	for(int i=2;i<=60000;++i)
	{
		if(!vis[i]) prime[++len]=i;
		for(int j=1;j<=len&&1ll*prime[j]*i<=60000;++j)
		{
			vis[prime[j]*i]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int t;
long long ans;
struct node
{
	int x,y;
} a[8005];
int s[8005];
int dis[4005][4005];
int n,m;
int TH;
signed main()
{
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	sss();
	t=read();
	while(t--)
	{
		ans=0;TH=0;
		n=read(),m=read();
		for(int i=1;i<=n;++i) a[i].x=read(),a[i].y=read();
		for(int i=1;i<=n;++i)
		{
			s[i]=read();
			if(s[i]!=1) TH=1; 
		}
		for(int i=1;i<=n;++i)
		   for(int j=i+1;j<=n;++j)
		      dis[i][j]=dis[j][i]=abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y);
		if(n<=100)
		{
			for(int i=1;i<=n;++i)
			   for(int j=i+1;j<=n;++j)
			      for(int k=j+1;k<=n;++k)
			      {
			      	int x=dis[i][j],y=dis[j][k],z=dis[i][k];
			      	if(x<y) swap(x,y);
			      	if(x<z) swap(x,z);
			      	if(y<z) swap(y,z);
			      	if(!vis[y]&&(y%20==3||y<=10))
			      	{
			      		ans+=s[i]+s[j]+s[k];
					  }
				  }
		}
		printf("%lld\n",ans);
	}
	return 0;
}

